def getText():
    txt = open("../../data/Mdata/test.txt", "r", encoding='utf-8').read()  # 读取Hamlet文本文件，并返回给txt
    txt = txt.lower()  # 将文件中的单词全部变为小写
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
        txt = txt.replace(ch, " ")  # 将文本中特殊字符替换为空格
    return txt


hamletTxt = getText()
words = hamletTxt.split()  # 按照空格，将文本分割
counts = {}
for word in words:  # 统计单词出现的次数，并存储到counts字典中
    counts[word] = counts.get(word, 0) + 1  # 先给字典赋值，如果字典中没有word这个键，则返回0
items = list(counts.items())  # 将字典转换为列表，以便操作
items.sort(key=lambda x: x[1], reverse=True)  # 见下面函数讲解
for tuple1 in items:
    word, count = tuple1[0],tuple1[1]
    print(word, count)
